<!-- 分类商品列表 -->
<template>
	<view class="zg-area">
		<!-- 导航 -->
		<view class="fixed-tool-top">
			<!-- 分类2 -->
			<Typebar :list="nav2" :currentIndex="currentIndex2" @on-change="changeTab2" />
			<!-- 筛选 -->
			<TypeFilter @open-filter="show1=true" :isFilt="false" @type-change="changeTab3" @row-change="changeTab4" />
		</view>
		<view class="content">
			<view class="area-1" v-if="list.length > 0">
				<view v-if="row_type === 'row'">
					<view class="card" v-for="(item,index) in list" :key="index" @click="goRouter1(item)">
						<view class="card-left">
							<image :src="`${shopImgUrl}${item.pic}`" mode="widthFix"></image>
						</view>
						<view class="card-right">
							<view class="card-title ">
								<text class="ellispsis1">{{item.name || '商品名称'}}</text> 
							</view>
							<view class="card-info">
								<view class="card-price">
									<text>￥{{item.discountPrice || 0}}</text>
									<text>￥{{item.price || 0}}</text>
								</view>
								<view class="card-xl">
									销量{{handleSales(item.sale)}}
								</view>
							</view>
						</view>
					</view>
				</view>
				<view class="card-wapper" v-else>
					<view class="card" v-for="(item,index) in list" :key="index" @click="goRouter1(item)">
						<view class="card-top">
							<image :src="`${shopImgUrl}${item.pic}`" mode="aspectFill"></image>
						</view>
						<view class="card-bottom">
							<view class="card-title ellispsis1">
								{{item.name || '商品名称'}}
							</view>
							<view class="card-info">
								<view class="card-price">
									<text>￥{{item.discountPrice || 0}}</text>
									<text>￥{{item.price || 0}}</text>
								</view>
								<view class="card-xl">
									销量{{handleSales(item.sale)}}
								</view>
							</view>
						</view>
					</view>
				</view>
				<uni-load-more color="#999" :status="status" v-if="list.length > 0"></uni-load-more>
			</view>
			<view class="nomore" v-if="list.length === 0 && !loading">
				<view class="nomore-text">
					<image src="/pagesShop/static/zanwu.png" mode="widthFix"></image>
					<view>暂无数据</view>
				</view>
			</view>
		</view>
		<!-- 筛选 -->
		<!-- <FilterModal v-model="show1" :area="[0,1,1,1]" @on-confirm="confirmFilter" @on-reset="resetFilter" /> -->
	</view>
</template>

<script>
	// import FilterModal from '../common/FilterModal.vue';
	import Typebar from '../common/Typebar.vue';
	import TypeFilter from '../common/TypeFilter.vue';
	
	export default {
		components: {
			// FilterModal,
			Typebar,
			TypeFilter
		},
		data() {
			return {
				show1: false,
				row_type: 'table',
				currentIndex2: 0,
				type: '0',
				loading: false,
				page: 1,
				status: 'more',
				list: [],
				firstCategoryId: '', // 一级分类id
				categoryId: '', // 二级分类id
				nav2: [], // 二级分类列表
				sortx: 1,
				filters: {
					price1: '', // 最低价
					price2: '', // 最高价
					good: '', // 好评
					address: [],
				},
			}
		},
		// params: nav1_id 、nav1_name
		onLoad(params) {
			console.log(params);
			this.firstCategoryId = parseInt(params.nav1_id) || ''
			uni.setNavigationBarTitle({
				title: params.nav1_name || ''
			})
			this.getList1()
			this.getList2()
		},
		onPullDownRefresh() {
			this.getList1()
		},
		onReachBottom() {
			this.getList1_1()
		},
		methods: {
			// 根据条件查询商品
			getList1() {
				this.page = 1
				this.status = 'more'
				this.loading = true
				this.$http.post(this.$api.shop_list_1, {}, {
					params: {
						firstCategoryId: this.firstCategoryId, // 商品一级分类id
						categoryId: this.categoryId, // 商品二级分类id
						pageNum: this.page,
						status: this.type, // 0综合 1销量 2价格
						sort: this.sortx, // 0升序 1降序
						type: 0,
						floorPrice: this.filters.price1, // 最低价
						highPrice: this.filters.price2, // 最高价
						praiseRate: this.filters.good, // 好评率
						area: this.filters.address.join('-'), // 地区
					}
				}).then(result => {
					uni.stopPullDownRefresh()
					this.loading = false
					if (result.code === 200) {
						const list = result.data.result;
						if (Array.isArray(list) && list.length > 0) {
							this.list = list
							if (this.list.length < 10) {
								this.status = 'noMore'
							}
						} else {
							this.list = []
							this.status = 'more'
						}
					} else {
						this.list = []
						this.status = 'more'
					}
				}).catch(() => {
					uni.stopPullDownRefresh()
					this.loading = false
					this.list = []
					this.status = 'more'
				})
			},
			// 根据条件查询商品
			getList1_1() {
				if (this.status === 'noMore') return
				this.page++
				this.status = 'loading'
				this.$http.post(this.$api.shop_list_1, {}, {
					params: {
						firstCategoryId: this.firstCategoryId, // 商品一级分类id
						categoryId: this.categoryId, // 商品二级分类id
						pageNum: this.page,
						status: this.type, // 0综合 1销量 2价格
						sort: this.sortx, // 0升序 1降序
						type: 0,
						floorPrice: this.filters.price1, // 最低价
						highPrice: this.filters.price2, // 最高价
						praiseRate: this.filters.good, // 好评率
						area: this.filters.address.join('-'), // 地区
					}
				}).then(result => {
					if (result.code === 200) {
						const list = result.data.result;
						if (Array.isArray(list)) {
							if (list.length > 0) {
								this.list = this.list.concat(list);
							} else {
								this.status = 'noMore'
							}
						} else {
							this.status = 'more'
						}
					}
				}).catch(() => {
					this.status = 'more'
				})
			},
			// 获取商品全部类目
			getList2() {
				this.$http.post(this.$api.shop_tabs_1).then(result => {
					if (result.code === 200) {
						const nav1 = result.data; // 一级分类
						if(Array.isArray(nav1) && nav1.length > 0) {
							nav1.map((item,index) => {
								if (
									item.id === this.firstCategoryId
									&& Array.isArray(item.mallProductCategories) 
									&& item.mallProductCategories.length > 0
								) {
									this.nav2 = [{name: '全部'}].concat(item.mallProductCategories) // 二级分类
								}
							})
						} else {
							this.nav2 = []
						}
					} else {
						this.nav2 = []
					}
				}).catch(() => {
					this.nav2 = []
				})
			},
			// 跳转：商品详情
			goRouter1(item) {
				uni.navigateTo({
					url: `../details/details?cid=${item.id}`
				});
			},
			// 商品分类：选择二级分类
			changeTab2({item,index}) {
				this.currentIndex2 = index
				this.categoryId = index > 0 ? item.id : ''
				this.getList1()
			},
			// 排序：切换类型
			changeTab3({index,sort}) {
				this.type = index
				this.sortx = sort
				this.getList1()
			},
			// 排版：切换
			changeTab4(row_type) {
				this.row_type = row_type
			},
			// 筛选：确认
			confirmFilter(data) {
				this.filters = {
					price1: data.price1,
					price2: data.price2,
					good: data.good,
					address: data.address,
				}
				this.getList1()
			},
			resetFilter() {
				this.filters = {
					price1: '',
					price2: '',
					good: '',
					address: [],
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.card-info-right {
		display: flex;

		&>view {
			margin-left: 10rpx;
		}
	}

	.card {
		margin: 30rpx;
		padding: 20rpx;
		background-color: #fff;
		border-radius: 10rpx;
		display: flex;
		align-items: center;
	}

	.card-left {
		flex-shrink: 0;
		width: 180rpx;
		height: 180rpx;
		overflow: hidden;
		margin-right: 20rpx;

		image {
			width: 100%;
			height: auto;
		}
	}

	.card-right {
		flex-grow: 1;
	}

	.card-title {
		font-size: 28rpx;
	}

	.card-info {
		margin-top: 30rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.card-price {
		text {
			&:nth-of-type(1) {
				color: $uni-color-error;
				font-size: 36rpx;
				font-weight: bold;
			}

			&:nth-of-type(2) {
				margin-left: 10rpx;
				color: #999;
				text-decoration: line-through;
			}
		}
	}

	.card-xl {
		font-size: 24rpx;
		color: #999;
	}

	.card-tools {
		margin-top: 10rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.card-time {
		margin-top: 20rpx;
		color: $uni-color-error;
		font-size: 24rpx;
	}

	.card-wapper {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
		margin: 30rpx;

		.card {
			margin: 0 0 30rpx;
			padding: 0;
			flex-direction: column;
			width: 330rpx;
		}

		.card-tools {
			flex-direction: column;
			align-items: flex-start;
			margin-bottom: 10rpx;
		}

		.card-info {
			margin-top: 10rpx;
		}

		.card-time {
			margin-top: 10rpx;
		}
	}

	.card-top {
		width: 330rpx;
		height: 330rpx;
		
		image {
			width: 330rpx;
			height: 330rpx;
			 border-radius: 12rpx 12rpx 0 0;
		}
	}

	.card-bottom {
		padding: 10rpx;
		width: 100%;
		box-sizing: border-box;
	}

	.fixed-tool-top {
		position: sticky;
		top: 0;
		left: 0;
		z-index: 1;
		width: 100%;
		box-sizing: border-box;
		background-color: #fff;
	}
</style>
